<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <div id="app">
      <div>
        <span @click="all">全选:</span>
        <input type="checkbox" v-model="allBox" />
        <button @click="counter">反选</button>
        <ul v-for="(item,index) in arr">
          <li>
            <input type="checkbox" v-model="item.checked" />
            <span>{{ item.name }}{{ item.checked }}</span>
          </li>
        </ul>
      </div>
    </div>
    <script src="./vue.js"></script>
    <script>
      const app = new Vue({
        el: "#app",
        data: {
          arr: [
            {
              name: "猪八戒",
              checked: false,
            },
            {
              name: "孙悟空",
              checked: false,
            },
            {
              name: "唐僧",
              checked: false,
            },
            {
              name: "白龙马",
              checked: false,
            },
          ],
          allBox: false,
        },
        methods: {
          all() {
            this.arr.forEach((element) => {
              element.checked = true;
              console.log(this.arr);
            });
            this.allBox = true;
          },
          counter() {
            if (this.allBox) {
              this.arr.forEach((element) => {
                element.checked = false;
              });
              this.allBox = false;
            } else {
              this.arr.forEach((element) => {
                element.checked = true;
              });
              this.allBox = true;
            }
          },
        },
        computed: {},
      });
    </script>
  </body>
</html>
